(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.apMemberEditController', apMemberEditController);
	apMemberEditController.$inject = [ '$scope', '$q', '$uibModalInstance', '$timeout', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig', 'apMemberService'];
	function apMemberEditController($scope, $q, $uibModalInstance, $timeout, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig, apMemberService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("apMemberEditController"), ajaxService = ajaxServiceFactory.buildAjaxService({
		});
		ctrl.dialogsModule = dialogsModule;
//		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.initValidatorConfig = angular.copy(ctrl.validatorConfig);
		ctrl.validatorConfig = {};
		ctrl.filterPrimaryKeys = ctrl.dialogsModule.params.filterPrimaryKeys || [];
		ctrl.isEdit = ctrl.dialogsModule.params.isEdit;
		ctrl.updateProcessFlag = ctrl.dialogsModule.params.updateProcessFlag;
		ctrl.apCommunityBuyerCode = ctrl.dialogsModule.params.apCommunityBuyerCode;
		ctrl.formName = "apMemberForm";
		ctrl.apMember = {};
		
		ctrl.init = function() {
			// init acct
			apMemberService.initProfAcct(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init int
			//apMemberService.initProfInt(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			//apMemberService.initProfCostOfFundInt(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init pastdue
			apMemberService.initProfPastdue(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init fee
			apMemberService.initProfFee(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init doc
			apMemberService.initEventDoccfg(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init limit facility
			//apMemberService.initProfLimitFaci(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			//apMemberService.initProfLimitInsFaci(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			//apMemberService.initProfLimitPurFaci(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init Settlement Instruction
			apMemberService.initProfSetlinst(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			// init cust prop define
			apMemberService.initCustProp(layoutConfig, $scope, ctrl, ctrl.updateProcessFlag);
			//
			apMemberService.initGeneral($scope, ctrl, ctrl.updateProcessFlag).then(function() {
				if (ctrl.dialogsModule.params.isAdd == 'Y') {
					authService.initApMember().then(function(response) {
						var initApMember = angular.merge(response.data, ctrl.dialogsModule.params.apMember || {});
						initApMember.prodSubtypeExtProperty = ctrl.dialogsModule.params.prodSubtypeExtProperty;
						initApMember.prodSubtype = initApMember.prodSubtypeExtProperty.prodSubtype;
						// reset data
						apMemberService.resetMemberByProdSubtypeExtProperty(ctrl, initApMember);
						apMemberService.initLayoutConfigAndModel(ctrl, $scope, initApMember, "apMemberEdit", ctrl.updateProcessFlag).then(function() {
							// reset cust prop data
							ctrl.resetCustProperties();
						});
					}, angular.noop);
				} else {
					var initApMember = ctrl.dialogsModule.params.apMember || {};
					initApMember.prodSubtypeExtProperty = ctrl.dialogsModule.params.prodSubtypeExtProperty;
					initApMember.prodSubtype = initApMember.prodSubtypeExtProperty.prodSubtype;
					// reset data
					apMemberService.resetMemberByProdSubtypeExtProperty(ctrl, initApMember);
					apMemberService.initLayoutConfigAndModel(ctrl, $scope, initApMember, "apMemberEdit", ctrl.updateProcessFlag).then(function() {
						// reset cust prop data
						ctrl.resetCustProperties();
					});
				}
			});
		};
		
		ctrl.ok = function() {
			apMemberService.clearDirtyData(ctrl);
			$timeout(function(){
				if ($scope.apMemberForm) {
					$scope.apMemberForm.$setSubmitted();
					if(!$scope.apMemberForm.$valid) {
						dialogsModule.showErrorMessages($scope.apMemberForm);
						return;
					}
				}
				if ((!$scope.apMemberForm || $scope.apMemberForm.$valid) && (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.apMember) !== false)) {
					$uibModalInstance.close();
				}
			});
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);
